{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Please input your directory for the top level folder\n",
    "folder name : SUBMISSION MODEL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_ = 'INPUT-PROJECT-DIRECTORY/submission_model/' # input only here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### setting other directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_dir = dir_+'2. data/'\n",
    "processed_data_dir = dir_+'2. data/processed/'\n",
    "log_dir = dir_+'4. logs/'\n",
    "model_dir = dir_+'5. models/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "####################################################################################\n",
    "################## 2-3. nonrecursive model by store & dept #########################\n",
    "####################################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cvs = ['private']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "STORES = ['CA_1', 'CA_2', 'CA_3', 'CA_4', 'TX_1', 'TX_2', 'TX_3', 'WI_1', 'WI_2', 'WI_3']\n",
    "DEPTS = ['HOBBIES_1', 'HOBBIES_2', 'HOUSEHOLD_1', 'HOUSEHOLD_2', 'FOODS_1', 'FOODS_2', 'FOODS_3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from  datetime import datetime, timedelta\n",
    "import gc\n",
    "import numpy as np, pandas as pd\n",
    "import lightgbm as lgb\n",
    "\n",
    "import os, sys, gc, time, warnings, pickle, psutil, random\n",
    "\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reduce_mem_usage(df, verbose=False):\n",
    "    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n",
    "    start_mem = df.memory_usage().sum() / 1024**2    \n",
    "    for col in df.columns:\n",
    "        col_type = df[col].dtypes\n",
    "        if col_type in numerics:\n",
    "            c_min = df[col].min()\n",
    "            c_max = df[col].max()\n",
    "            if str(col_type)[:3] == 'int':\n",
    "                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n",
    "                    df[col] = df[col].astype(np.int8)\n",
    "                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n",
    "                       df[col] = df[col].astype(np.int16)\n",
    "                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n",
    "                    df[col] = df[col].astype(np.int32)\n",
    "                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n",
    "                    df[col] = df[col].astype(np.int64)  \n",
    "            else:\n",
    "                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n",
    "                    df[col] = df[col].astype(np.float16)\n",
    "                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n",
    "                    df[col] = df[col].astype(np.float32)\n",
    "                else:\n",
    "                    df[col] = df[col].astype(np.float64)    \n",
    "    end_mem = df.memory_usage().sum() / 1024**2\n",
    "    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "FIRST_DAY = 710\n",
    "remove_feature = ['id',\n",
    "                  'state_id',\n",
    "                  'store_id',\n",
    "#                   'item_id',\n",
    "                  'dept_id',\n",
    "                  'cat_id',\n",
    "                  'date','wm_yr_wk','d','sales']\n",
    "\n",
    "cat_var = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + [\"event_name_1\", \"event_name_2\", \"event_type_1\", \"event_type_2\"]\n",
    "cat_var = list(set(cat_var) - set(remove_feature))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid2_colnm = ['sell_price', 'price_max', 'price_min', 'price_std',\n",
    "               'price_mean', 'price_norm', 'price_nunique', 'item_nunique',\n",
    "               'price_momentum', 'price_momentum_m', 'price_momentum_y']\n",
    "\n",
    "grid3_colnm = ['event_name_1', 'event_type_1', 'event_name_2',\n",
    "               'event_type_2', 'snap_CA', 'snap_TX', 'snap_WI', 'tm_d', 'tm_w', 'tm_m',\n",
    "               'tm_y', 'tm_wm', 'tm_dw', 'tm_w_end']\n",
    "\n",
    "lag_colnm = [ 'sales_lag_28', 'sales_lag_29', 'sales_lag_30',\n",
    "             'sales_lag_31', 'sales_lag_32', 'sales_lag_33', 'sales_lag_34',\n",
    "             'sales_lag_35', 'sales_lag_36', 'sales_lag_37', 'sales_lag_38',\n",
    "             'sales_lag_39', 'sales_lag_40', 'sales_lag_41', 'sales_lag_42',\n",
    "             \n",
    "             'rolling_mean_7', 'rolling_std_7', 'rolling_mean_14', 'rolling_std_14',\n",
    "             'rolling_mean_30', 'rolling_std_30', 'rolling_mean_60',\n",
    "             'rolling_std_60', 'rolling_mean_180', 'rolling_std_180']\n",
    "\n",
    "mean_enc_colnm = [\n",
    "    \n",
    "    'enc_item_id_store_id_mean', 'enc_item_id_store_id_std'\n",
    "\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "########################### Make grid\n",
    "#################################################################################\n",
    "def prepare_data(store, state):\n",
    "    \n",
    "    grid_1 = pd.read_pickle(processed_data_dir+\"grid_part_1.pkl\")\n",
    "    grid_2 = pd.read_pickle(processed_data_dir+\"grid_part_2.pkl\")[grid2_colnm]\n",
    "    grid_3 = pd.read_pickle(processed_data_dir+\"grid_part_3.pkl\")[grid3_colnm]\n",
    "\n",
    "    grid_df = pd.concat([grid_1, grid_2, grid_3], axis=1)\n",
    "    del grid_1, grid_2, grid_3; gc.collect()\n",
    "    \n",
    "    grid_df = grid_df[(grid_df['store_id'] == store) & (grid_df['dept_id'] == state)]\n",
    "    grid_df = grid_df[grid_df['d'] >= FIRST_DAY]\n",
    "    \n",
    "    lag = pd.read_pickle(processed_data_dir+\"lags_df_28.pkl\")[lag_colnm]\n",
    "    \n",
    "    lag = lag[lag.index.isin(grid_df.index)]\n",
    "    \n",
    "    grid_df = pd.concat([grid_df,\n",
    "                     lag],\n",
    "                    axis=1)\n",
    "    \n",
    "    del lag; gc.collect()\n",
    "    \n",
    "\n",
    "    mean_enc = pd.read_pickle(processed_data_dir+\"mean_encoding_df.pkl\")[mean_enc_colnm]\n",
    "    mean_enc = mean_enc[mean_enc.index.isin(grid_df.index)]\n",
    "    \n",
    "    grid_df = pd.concat([grid_df,\n",
    "                         mean_enc],\n",
    "                        axis=1)    \n",
    "    del mean_enc; gc.collect()\n",
    "    \n",
    "    grid_df = reduce_mem_usage(grid_df)\n",
    "    \n",
    "    \n",
    "    \n",
    "    return grid_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "validation = {\n",
    "    'cv1' : [1551, 1610],\n",
    "    'cv2' : [1829,1857],\n",
    "    'cv3' : [1857, 1885],\n",
    "    'cv4' : [1885,1913],\n",
    "    'public' : [1913, 1941],\n",
    "    'private' : [1941, 1969]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cv1 : 2015-04-28 ~ 2015-06-26\n",
    "\n",
    "### cv2 : 2016-02-01 ~ 2016-02-28\n",
    "\n",
    "### cv3 : 2016-02-29 ~ 2016-03-27\n",
    "\n",
    "### cv4 : 2016-03-28 ~ 2016-04-24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "########################### Model params\n",
    "#################################################################################\n",
    "lgb_params = {\n",
    "                    'boosting_type': 'gbdt',\n",
    "                    'objective': 'tweedie',\n",
    "                    'tweedie_variance_power': 1.1,\n",
    "                    'metric': 'rmse',\n",
    "                    'subsample': 0.5,\n",
    "                    'subsample_freq': 1,\n",
    "                    'learning_rate': 0.015,\n",
    "                    'num_leaves': 2**8-1,\n",
    "                    'min_data_in_leaf': 2**8-1,\n",
    "                    'feature_fraction': 0.5,\n",
    "                    'max_bin': 100,\n",
    "                    'n_estimators': 3000,\n",
    "                    'boost_from_average': False,\n",
    "                    'verbose': -1,\n",
    "                    'seed' : 1995\n",
    "                } "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cv : day [1941, 1969]\n",
      "CA_1 HOBBIES_1 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.76965\tvalid_1's rmse: 2.56407\n",
      "[200]\tvalid_0's rmse: 2.1041\tvalid_1's rmse: 2.45938\n",
      "[300]\tvalid_0's rmse: 2.19545\tvalid_1's rmse: 2.40284\n",
      "[400]\tvalid_0's rmse: 2.21621\tvalid_1's rmse: 2.35022\n",
      "[500]\tvalid_0's rmse: 2.21806\tvalid_1's rmse: 2.30324\n",
      "[600]\tvalid_0's rmse: 2.22061\tvalid_1's rmse: 2.25746\n",
      "[700]\tvalid_0's rmse: 2.22182\tvalid_1's rmse: 2.21407\n",
      "[800]\tvalid_0's rmse: 2.22035\tvalid_1's rmse: 2.17136\n",
      "[900]\tvalid_0's rmse: 2.21246\tvalid_1's rmse: 2.12996\n",
      "[1000]\tvalid_0's rmse: 2.21424\tvalid_1's rmse: 2.08958\n",
      "[1100]\tvalid_0's rmse: 2.20179\tvalid_1's rmse: 2.04951\n",
      "[1200]\tvalid_0's rmse: 2.20023\tvalid_1's rmse: 2.01059\n",
      "[1300]\tvalid_0's rmse: 2.19463\tvalid_1's rmse: 1.97334\n",
      "[1400]\tvalid_0's rmse: 2.18705\tvalid_1's rmse: 1.93599\n",
      "[1500]\tvalid_0's rmse: 2.18653\tvalid_1's rmse: 1.89932\n",
      "[1600]\tvalid_0's rmse: 2.18435\tvalid_1's rmse: 1.8648\n",
      "[1700]\tvalid_0's rmse: 2.17788\tvalid_1's rmse: 1.83184\n",
      "[1800]\tvalid_0's rmse: 2.17166\tvalid_1's rmse: 1.79881\n",
      "[1900]\tvalid_0's rmse: 2.16999\tvalid_1's rmse: 1.76683\n",
      "[2000]\tvalid_0's rmse: 2.16326\tvalid_1's rmse: 1.73588\n",
      "[2100]\tvalid_0's rmse: 2.15705\tvalid_1's rmse: 1.70673\n",
      "[2200]\tvalid_0's rmse: 2.15542\tvalid_1's rmse: 1.67798\n",
      "[2300]\tvalid_0's rmse: 2.14649\tvalid_1's rmse: 1.65113\n",
      "[2400]\tvalid_0's rmse: 2.14322\tvalid_1's rmse: 1.62333\n",
      "[2500]\tvalid_0's rmse: 2.14063\tvalid_1's rmse: 1.59747\n",
      "[2600]\tvalid_0's rmse: 2.13729\tvalid_1's rmse: 1.57314\n",
      "[2700]\tvalid_0's rmse: 2.13084\tvalid_1's rmse: 1.55028\n",
      "[2800]\tvalid_0's rmse: 2.12542\tvalid_1's rmse: 1.52768\n",
      "[2900]\tvalid_0's rmse: 2.1187\tvalid_1's rmse: 1.50567\n",
      "[3000]\tvalid_0's rmse: 2.11318\tvalid_1's rmse: 1.48389\n",
      "CA_1 HOBBIES_2 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 0.453784\tvalid_1's rmse: 0.807891\n",
      "[200]\tvalid_0's rmse: 0.366129\tvalid_1's rmse: 0.779944\n",
      "[300]\tvalid_0's rmse: 0.353711\tvalid_1's rmse: 0.764756\n",
      "[400]\tvalid_0's rmse: 0.351489\tvalid_1's rmse: 0.748572\n",
      "[500]\tvalid_0's rmse: 0.350767\tvalid_1's rmse: 0.732544\n",
      "[600]\tvalid_0's rmse: 0.349771\tvalid_1's rmse: 0.716688\n",
      "[700]\tvalid_0's rmse: 0.346499\tvalid_1's rmse: 0.699318\n",
      "[800]\tvalid_0's rmse: 0.344468\tvalid_1's rmse: 0.682279\n",
      "[900]\tvalid_0's rmse: 0.342575\tvalid_1's rmse: 0.665661\n",
      "[1000]\tvalid_0's rmse: 0.339414\tvalid_1's rmse: 0.650279\n",
      "[1100]\tvalid_0's rmse: 0.337555\tvalid_1's rmse: 0.634153\n",
      "[1200]\tvalid_0's rmse: 0.337272\tvalid_1's rmse: 0.619718\n",
      "[1300]\tvalid_0's rmse: 0.334272\tvalid_1's rmse: 0.605952\n",
      "[1400]\tvalid_0's rmse: 0.332115\tvalid_1's rmse: 0.591993\n",
      "[1500]\tvalid_0's rmse: 0.32973\tvalid_1's rmse: 0.580362\n",
      "[1600]\tvalid_0's rmse: 0.327787\tvalid_1's rmse: 0.567644\n",
      "[1700]\tvalid_0's rmse: 0.325118\tvalid_1's rmse: 0.555691\n",
      "[1800]\tvalid_0's rmse: 0.323695\tvalid_1's rmse: 0.544674\n",
      "[1900]\tvalid_0's rmse: 0.321749\tvalid_1's rmse: 0.533783\n",
      "[2000]\tvalid_0's rmse: 0.320677\tvalid_1's rmse: 0.523208\n",
      "[2100]\tvalid_0's rmse: 0.318082\tvalid_1's rmse: 0.513799\n",
      "[2200]\tvalid_0's rmse: 0.316453\tvalid_1's rmse: 0.504853\n",
      "[2300]\tvalid_0's rmse: 0.315037\tvalid_1's rmse: 0.495936\n",
      "[2400]\tvalid_0's rmse: 0.312308\tvalid_1's rmse: 0.487394\n",
      "[2500]\tvalid_0's rmse: 0.311176\tvalid_1's rmse: 0.478998\n",
      "[2600]\tvalid_0's rmse: 0.309406\tvalid_1's rmse: 0.471542\n",
      "[2700]\tvalid_0's rmse: 0.308266\tvalid_1's rmse: 0.464498\n",
      "[2800]\tvalid_0's rmse: 0.306709\tvalid_1's rmse: 0.457341\n",
      "[2900]\tvalid_0's rmse: 0.303948\tvalid_1's rmse: 0.451229\n",
      "[3000]\tvalid_0's rmse: 0.303229\tvalid_1's rmse: 0.444952\n",
      "CA_1 HOUSEHOLD_1 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.85957\tvalid_1's rmse: 1.71035\n",
      "[200]\tvalid_0's rmse: 2.22227\tvalid_1's rmse: 1.61417\n",
      "[300]\tvalid_0's rmse: 2.3197\tvalid_1's rmse: 1.58187\n",
      "[400]\tvalid_0's rmse: 2.34264\tvalid_1's rmse: 1.55793\n",
      "[500]\tvalid_0's rmse: 2.34386\tvalid_1's rmse: 1.53757\n",
      "[600]\tvalid_0's rmse: 2.34311\tvalid_1's rmse: 1.52053\n",
      "[700]\tvalid_0's rmse: 2.34718\tvalid_1's rmse: 1.50449\n",
      "[800]\tvalid_0's rmse: 2.34923\tvalid_1's rmse: 1.48936\n",
      "[900]\tvalid_0's rmse: 2.35078\tvalid_1's rmse: 1.47434\n",
      "[1000]\tvalid_0's rmse: 2.35031\tvalid_1's rmse: 1.4614\n",
      "[1100]\tvalid_0's rmse: 2.34755\tvalid_1's rmse: 1.44728\n",
      "[1200]\tvalid_0's rmse: 2.34633\tvalid_1's rmse: 1.43382\n",
      "[1300]\tvalid_0's rmse: 2.34644\tvalid_1's rmse: 1.42068\n",
      "[1400]\tvalid_0's rmse: 2.34977\tvalid_1's rmse: 1.40822\n",
      "[1500]\tvalid_0's rmse: 2.34673\tvalid_1's rmse: 1.3966\n",
      "[1600]\tvalid_0's rmse: 2.34625\tvalid_1's rmse: 1.38549\n",
      "[1700]\tvalid_0's rmse: 2.34284\tvalid_1's rmse: 1.37413\n",
      "[1800]\tvalid_0's rmse: 2.3385\tvalid_1's rmse: 1.36491\n",
      "[1900]\tvalid_0's rmse: 2.33933\tvalid_1's rmse: 1.35438\n",
      "[2000]\tvalid_0's rmse: 2.34039\tvalid_1's rmse: 1.34573\n",
      "[2100]\tvalid_0's rmse: 2.3365\tvalid_1's rmse: 1.33653\n",
      "[2200]\tvalid_0's rmse: 2.33513\tvalid_1's rmse: 1.32772\n",
      "[2300]\tvalid_0's rmse: 2.33538\tvalid_1's rmse: 1.31956\n",
      "[2400]\tvalid_0's rmse: 2.33468\tvalid_1's rmse: 1.31165\n",
      "[2500]\tvalid_0's rmse: 2.33789\tvalid_1's rmse: 1.3043\n",
      "[2600]\tvalid_0's rmse: 2.33745\tvalid_1's rmse: 1.29666\n",
      "[2700]\tvalid_0's rmse: 2.33647\tvalid_1's rmse: 1.29012\n",
      "[2800]\tvalid_0's rmse: 2.33679\tvalid_1's rmse: 1.28307\n",
      "[2900]\tvalid_0's rmse: 2.33416\tvalid_1's rmse: 1.27584\n",
      "[3000]\tvalid_0's rmse: 2.33343\tvalid_1's rmse: 1.26903\n",
      "CA_1 HOUSEHOLD_2 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 0.648541\tvalid_1's rmse: 0.75603\n",
      "[200]\tvalid_0's rmse: 0.629132\tvalid_1's rmse: 0.733861\n",
      "[300]\tvalid_0's rmse: 0.632023\tvalid_1's rmse: 0.726387\n",
      "[400]\tvalid_0's rmse: 0.635702\tvalid_1's rmse: 0.719737\n",
      "[500]\tvalid_0's rmse: 0.636308\tvalid_1's rmse: 0.713797\n",
      "[600]\tvalid_0's rmse: 0.636786\tvalid_1's rmse: 0.708421\n",
      "[700]\tvalid_0's rmse: 0.636572\tvalid_1's rmse: 0.703399\n",
      "[800]\tvalid_0's rmse: 0.63555\tvalid_1's rmse: 0.698588\n",
      "[900]\tvalid_0's rmse: 0.635066\tvalid_1's rmse: 0.693958\n",
      "[1000]\tvalid_0's rmse: 0.635349\tvalid_1's rmse: 0.689478\n",
      "[1100]\tvalid_0's rmse: 0.634577\tvalid_1's rmse: 0.685216\n",
      "[1200]\tvalid_0's rmse: 0.633062\tvalid_1's rmse: 0.681128\n",
      "[1300]\tvalid_0's rmse: 0.633471\tvalid_1's rmse: 0.67708\n",
      "[1400]\tvalid_0's rmse: 0.633273\tvalid_1's rmse: 0.673182\n",
      "[1500]\tvalid_0's rmse: 0.633224\tvalid_1's rmse: 0.669301\n",
      "[1600]\tvalid_0's rmse: 0.631919\tvalid_1's rmse: 0.665465\n",
      "[1700]\tvalid_0's rmse: 0.631622\tvalid_1's rmse: 0.661842\n",
      "[1800]\tvalid_0's rmse: 0.631468\tvalid_1's rmse: 0.658217\n",
      "[1900]\tvalid_0's rmse: 0.63061\tvalid_1's rmse: 0.654433\n",
      "[2000]\tvalid_0's rmse: 0.630304\tvalid_1's rmse: 0.6511\n",
      "[2100]\tvalid_0's rmse: 0.629596\tvalid_1's rmse: 0.647641\n",
      "[2200]\tvalid_0's rmse: 0.62955\tvalid_1's rmse: 0.644195\n",
      "[2300]\tvalid_0's rmse: 0.629738\tvalid_1's rmse: 0.640875\n",
      "[2400]\tvalid_0's rmse: 0.628276\tvalid_1's rmse: 0.63754\n",
      "[2500]\tvalid_0's rmse: 0.627324\tvalid_1's rmse: 0.634358\n",
      "[2600]\tvalid_0's rmse: 0.627123\tvalid_1's rmse: 0.631097\n",
      "[2700]\tvalid_0's rmse: 0.627428\tvalid_1's rmse: 0.628039\n",
      "[2800]\tvalid_0's rmse: 0.626998\tvalid_1's rmse: 0.624784\n",
      "[2900]\tvalid_0's rmse: 0.626918\tvalid_1's rmse: 0.621821\n",
      "[3000]\tvalid_0's rmse: 0.626753\tvalid_1's rmse: 0.618778\n",
      "CA_1 FOODS_1 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.9276\tvalid_1's rmse: 2.27645\n",
      "[200]\tvalid_0's rmse: 2.2733\tvalid_1's rmse: 2.11139\n",
      "[300]\tvalid_0's rmse: 2.35239\tvalid_1's rmse: 2.03091\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[400]\tvalid_0's rmse: 2.37144\tvalid_1's rmse: 1.97145\n",
      "[500]\tvalid_0's rmse: 2.35977\tvalid_1's rmse: 1.92399\n",
      "[600]\tvalid_0's rmse: 2.35328\tvalid_1's rmse: 1.88372\n",
      "[700]\tvalid_0's rmse: 2.34849\tvalid_1's rmse: 1.84789\n",
      "[800]\tvalid_0's rmse: 2.33856\tvalid_1's rmse: 1.81521\n",
      "[900]\tvalid_0's rmse: 2.33635\tvalid_1's rmse: 1.78406\n",
      "[1000]\tvalid_0's rmse: 2.32503\tvalid_1's rmse: 1.75536\n",
      "[1100]\tvalid_0's rmse: 2.31868\tvalid_1's rmse: 1.72732\n",
      "[1200]\tvalid_0's rmse: 2.31701\tvalid_1's rmse: 1.70153\n",
      "[1300]\tvalid_0's rmse: 2.31052\tvalid_1's rmse: 1.676\n",
      "[1400]\tvalid_0's rmse: 2.30551\tvalid_1's rmse: 1.65112\n",
      "[1500]\tvalid_0's rmse: 2.29228\tvalid_1's rmse: 1.62783\n",
      "[1600]\tvalid_0's rmse: 2.2864\tvalid_1's rmse: 1.60531\n",
      "[1700]\tvalid_0's rmse: 2.28195\tvalid_1's rmse: 1.58323\n",
      "[1800]\tvalid_0's rmse: 2.28022\tvalid_1's rmse: 1.56198\n",
      "[1900]\tvalid_0's rmse: 2.27783\tvalid_1's rmse: 1.54164\n",
      "[2000]\tvalid_0's rmse: 2.27543\tvalid_1's rmse: 1.52168\n",
      "[2100]\tvalid_0's rmse: 2.27075\tvalid_1's rmse: 1.50265\n",
      "[2200]\tvalid_0's rmse: 2.26859\tvalid_1's rmse: 1.48387\n",
      "[2300]\tvalid_0's rmse: 2.26616\tvalid_1's rmse: 1.46567\n",
      "[2400]\tvalid_0's rmse: 2.26232\tvalid_1's rmse: 1.44825\n",
      "[2500]\tvalid_0's rmse: 2.25531\tvalid_1's rmse: 1.43156\n",
      "[2600]\tvalid_0's rmse: 2.24946\tvalid_1's rmse: 1.4148\n",
      "[2700]\tvalid_0's rmse: 2.2452\tvalid_1's rmse: 1.39866\n",
      "[2800]\tvalid_0's rmse: 2.23677\tvalid_1's rmse: 1.38312\n",
      "[2900]\tvalid_0's rmse: 2.23068\tvalid_1's rmse: 1.36829\n",
      "[3000]\tvalid_0's rmse: 2.22949\tvalid_1's rmse: 1.3536\n",
      "CA_1 FOODS_2 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.8157\tvalid_1's rmse: 1.91611\n",
      "[200]\tvalid_0's rmse: 2.17638\tvalid_1's rmse: 1.76177\n",
      "[300]\tvalid_0's rmse: 2.27862\tvalid_1's rmse: 1.70304\n",
      "[400]\tvalid_0's rmse: 2.31407\tvalid_1's rmse: 1.66005\n",
      "[500]\tvalid_0's rmse: 2.33059\tvalid_1's rmse: 1.62506\n",
      "[600]\tvalid_0's rmse: 2.3493\tvalid_1's rmse: 1.597\n",
      "[700]\tvalid_0's rmse: 2.34579\tvalid_1's rmse: 1.57288\n",
      "[800]\tvalid_0's rmse: 2.35208\tvalid_1's rmse: 1.55191\n",
      "[900]\tvalid_0's rmse: 2.35954\tvalid_1's rmse: 1.53274\n",
      "[1000]\tvalid_0's rmse: 2.36327\tvalid_1's rmse: 1.51497\n",
      "[1100]\tvalid_0's rmse: 2.36094\tvalid_1's rmse: 1.49785\n",
      "[1200]\tvalid_0's rmse: 2.36519\tvalid_1's rmse: 1.4819\n",
      "[1300]\tvalid_0's rmse: 2.36653\tvalid_1's rmse: 1.46686\n",
      "[1400]\tvalid_0's rmse: 2.36728\tvalid_1's rmse: 1.45258\n",
      "[1500]\tvalid_0's rmse: 2.36542\tvalid_1's rmse: 1.43855\n",
      "[1600]\tvalid_0's rmse: 2.36778\tvalid_1's rmse: 1.42517\n",
      "[1700]\tvalid_0's rmse: 2.36768\tvalid_1's rmse: 1.41244\n",
      "[1800]\tvalid_0's rmse: 2.36887\tvalid_1's rmse: 1.40049\n",
      "[1900]\tvalid_0's rmse: 2.36547\tvalid_1's rmse: 1.38863\n",
      "[2000]\tvalid_0's rmse: 2.3635\tvalid_1's rmse: 1.37735\n",
      "[2100]\tvalid_0's rmse: 2.36051\tvalid_1's rmse: 1.36658\n",
      "[2200]\tvalid_0's rmse: 2.36196\tvalid_1's rmse: 1.35601\n",
      "[2300]\tvalid_0's rmse: 2.35978\tvalid_1's rmse: 1.34554\n",
      "[2400]\tvalid_0's rmse: 2.35888\tvalid_1's rmse: 1.3356\n",
      "[2500]\tvalid_0's rmse: 2.36174\tvalid_1's rmse: 1.32554\n",
      "[2600]\tvalid_0's rmse: 2.36276\tvalid_1's rmse: 1.31611\n",
      "[2700]\tvalid_0's rmse: 2.36434\tvalid_1's rmse: 1.30699\n",
      "[2800]\tvalid_0's rmse: 2.36339\tvalid_1's rmse: 1.2982\n",
      "[2900]\tvalid_0's rmse: 2.36495\tvalid_1's rmse: 1.2893\n",
      "[3000]\tvalid_0's rmse: 2.36494\tvalid_1's rmse: 1.2806\n",
      "CA_1 FOODS_3 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 3.95388\tvalid_1's rmse: 4.24126\n",
      "[200]\tvalid_0's rmse: 5.06084\tvalid_1's rmse: 3.64788\n",
      "[300]\tvalid_0's rmse: 5.35452\tvalid_1's rmse: 3.42608\n",
      "[400]\tvalid_0's rmse: 5.42288\tvalid_1's rmse: 3.29501\n",
      "[500]\tvalid_0's rmse: 5.42921\tvalid_1's rmse: 3.19903\n",
      "[600]\tvalid_0's rmse: 5.42933\tvalid_1's rmse: 3.12639\n",
      "[700]\tvalid_0's rmse: 5.41677\tvalid_1's rmse: 3.06677\n",
      "[800]\tvalid_0's rmse: 5.40944\tvalid_1's rmse: 3.01175\n",
      "[900]\tvalid_0's rmse: 5.398\tvalid_1's rmse: 2.96162\n",
      "[1000]\tvalid_0's rmse: 5.38749\tvalid_1's rmse: 2.91997\n",
      "[1100]\tvalid_0's rmse: 5.36596\tvalid_1's rmse: 2.87829\n",
      "[1200]\tvalid_0's rmse: 5.35074\tvalid_1's rmse: 2.84018\n",
      "[1300]\tvalid_0's rmse: 5.34259\tvalid_1's rmse: 2.80527\n",
      "[1400]\tvalid_0's rmse: 5.33682\tvalid_1's rmse: 2.77362\n",
      "[1500]\tvalid_0's rmse: 5.32668\tvalid_1's rmse: 2.74269\n",
      "[1600]\tvalid_0's rmse: 5.32645\tvalid_1's rmse: 2.71494\n",
      "[1700]\tvalid_0's rmse: 5.31596\tvalid_1's rmse: 2.68839\n",
      "[1800]\tvalid_0's rmse: 5.30063\tvalid_1's rmse: 2.66217\n",
      "[1900]\tvalid_0's rmse: 5.29687\tvalid_1's rmse: 2.6382\n",
      "[2000]\tvalid_0's rmse: 5.29795\tvalid_1's rmse: 2.61487\n",
      "[2100]\tvalid_0's rmse: 5.28854\tvalid_1's rmse: 2.59242\n",
      "[2200]\tvalid_0's rmse: 5.2765\tvalid_1's rmse: 2.57132\n",
      "[2300]\tvalid_0's rmse: 5.26889\tvalid_1's rmse: 2.55099\n",
      "[400]\tvalid_0's rmse: 1.55043\tvalid_1's rmse: 1.82618\n",
      "[500]\tvalid_0's rmse: 1.55827\tvalid_1's rmse: 1.78814\n",
      "[600]\tvalid_0's rmse: 1.56286\tvalid_1's rmse: 1.75337\n",
      "[700]\tvalid_0's rmse: 1.56361\tvalid_1's rmse: 1.71682\n",
      "[800]\tvalid_0's rmse: 1.56051\tvalid_1's rmse: 1.68134\n",
      "[900]\tvalid_0's rmse: 1.55613\tvalid_1's rmse: 1.64692\n",
      "[1000]\tvalid_0's rmse: 1.54892\tvalid_1's rmse: 1.61397\n",
      "[1100]\tvalid_0's rmse: 1.54407\tvalid_1's rmse: 1.5812\n",
      "[1200]\tvalid_0's rmse: 1.53774\tvalid_1's rmse: 1.55069\n",
      "[1300]\tvalid_0's rmse: 1.53114\tvalid_1's rmse: 1.52135\n",
      "[1400]\tvalid_0's rmse: 1.5254\tvalid_1's rmse: 1.49045\n",
      "[1500]\tvalid_0's rmse: 1.52099\tvalid_1's rmse: 1.46161\n",
      "[1600]\tvalid_0's rmse: 1.51803\tvalid_1's rmse: 1.434\n",
      "[1700]\tvalid_0's rmse: 1.51468\tvalid_1's rmse: 1.40849\n",
      "[1800]\tvalid_0's rmse: 1.51044\tvalid_1's rmse: 1.38328\n",
      "[1900]\tvalid_0's rmse: 1.50746\tvalid_1's rmse: 1.35934\n",
      "[2000]\tvalid_0's rmse: 1.5036\tvalid_1's rmse: 1.33513\n",
      "[2100]\tvalid_0's rmse: 1.49961\tvalid_1's rmse: 1.3129\n",
      "[2200]\tvalid_0's rmse: 1.49561\tvalid_1's rmse: 1.2919\n",
      "[2600]\tvalid_0's rmse: 1.47615\tvalid_1's rmse: 1.21329\n",
      "[2700]\tvalid_0's rmse: 1.47279\tvalid_1's rmse: 1.19605\n",
      "[2800]\tvalid_0's rmse: 1.46905\tvalid_1's rmse: 1.17988\n",
      "[2900]\tvalid_0's rmse: 1.4664\tvalid_1's rmse: 1.1634\n",
      "[3000]\tvalid_0's rmse: 1.46358\tvalid_1's rmse: 1.14742\n",
      "CA_2 HOBBIES_2 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n"
     ]
    }
   ],
   "source": [
    "########################### Train Models\n",
    "#################################################################################\n",
    "from lightgbm import LGBMRegressor\n",
    "from gluonts.model.rotbaum._model import QRX\n",
    "rmsse_bycv = dict()\n",
    "\n",
    "for cv in cvs:\n",
    "    print('cv : day', validation[cv])\n",
    "\n",
    "    pred_list = []\n",
    "    for store in STORES:\n",
    "        for state in DEPTS:\n",
    "\n",
    "            print(store,state, 'start')\n",
    "            grid_df = prepare_data(store, state)\n",
    "\n",
    "            model_var = grid_df.columns[~grid_df.columns.isin(remove_feature)]\n",
    "\n",
    "            tr_mask = (grid_df['d'] <= validation[cv][0]) & (grid_df['d'] >= FIRST_DAY)\n",
    "            vl_mask = (grid_df['d'] > validation[cv][0]) & (grid_df['d'] <= validation[cv][1])\n",
    "\n",
    "            estimator = QRX(model=LGBMRegressor(**lgb_params),\n",
    "                        min_bin_size=200)\n",
    "            estimator.fit(\n",
    "                grid_df[tr_mask][model_var], \n",
    "                grid_df[tr_mask]['sales'],\n",
    "                max_sample_size=1000000, \n",
    "                seed=1,\n",
    "                eval_set=[(\n",
    "                        grid_df[vl_mask][model_var],\n",
    "                        grid_df[vl_mask]['sales']\n",
    "                    ),\n",
    "                    (\n",
    "                        grid_df[tr_mask][model_var], \n",
    "                        grid_df[tr_mask]['sales']\n",
    "                    )\n",
    "                ],\n",
    "                verbose=100,\n",
    "                x_train_is_dataframe=True\n",
    "            )\n",
    "            model_name = model_dir+'non_recur_model_'+store+'_'+state+'.bin'\n",
    "            pickle.dump(estimator, open(model_name, 'wb'))\n",
    "            \n",
    "#            display(pd.DataFrame({'name':m_lgb.feature_name(),\n",
    "#                                  'imp':m_lgb.feature_importance()}).sort_values('imp',ascending=False).head(25))\n",
    "            \n",
    "            del grid_df, estimator, tr_mask, vl_mask; gc.collect() #train_data, valid_data,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "conda_mxnet_p36",
   "language": "python",
   "name": "conda_mxnet_p36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
